using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Alogient.Cameleon.Sdk;
using Alogient.Cameleon.Forms.Sdk;
using Alogient.Cameleon.Forms.Sdk.Items;
using System.Web.UI;
using System.Drawing;

namespace Alogient.Cameleon.Forms.Sdk.Controls
{
    internal class FormRating : CMFormElement
    {
        public override string Value
        {
            get
            {
                var rating = new Rating(this.FormElementID, NavigationController.GetCultureCode());
                for (int i = 0; i < rating.Length; i++)
                {
                    RadioButton radio = this.FindControl(string.Format("rating_{0}_{1}", this.FormElementID.ToString(), (i + 1).ToString())) as RadioButton;
                    if (radio.Checked)
                    {
                        return (i + 1).ToString();
                    }
                }

                return "0";
            }
        }

        internal FormRating(Forms.Sdk.Items.Rating element) : base(element)
        {
            Table ratingTable = new Table();
            ratingTable.ID = string.Format("RatingTable_{0}", element.ID.ToString());
            
            TableRow labelRow = new TableRow();
            int middlepos = (int)element.Length / 2;

            TableRow numRow = new TableRow();

            TableRow radioRow = new TableRow();

            for (int i = 0; i < element.Length; i++)
            {
                System.Web.UI.WebControls.RadioButton radio = new RadioButton();
                radio.GroupName = element.ID.ToString();
                radio.ID = string.Format("rating_{0}_{1}", element.ID.ToString(), (i + 1).ToString());
                
                if ((i + 1) == element.DefaultValue)
                    radio.Checked = true;

                TableCell labelCell = new TableCell();
                TableCell numCell = new TableCell();
                TableCell radioCell = new TableCell();
                labelCell.HorizontalAlign = HorizontalAlign.Center;
                numCell.HorizontalAlign = HorizontalAlign.Center;
                radioCell.HorizontalAlign = HorizontalAlign.Center;

                Literal aLabel = new Literal();

                if (i == 0)
                    aLabel.Text = element.StartText;
                else if (i == middlepos)
                    aLabel.Text = element.MiddleText;
                else if (i + 1 == element.Length)
                    aLabel.Text = element.LastText;
                else
                    aLabel.Text = string.Empty;

                labelCell.Controls.Add(aLabel);
                Literal numLiteral = new Literal();
                numLiteral.Text = (i + 1).ToString();
                numCell.Controls.Add(numLiteral);
                radioCell.Controls.Add(radio);

                labelRow.Cells.Add(labelCell);
                numRow.Cells.Add(numCell);
                radioRow.Cells.Add(radioCell);
            }

            ratingTable.Rows.Add(labelRow);
            ratingTable.Rows.Add(numRow);
            ratingTable.Rows.Add(radioRow);

            Label label = new Label();
            label.Text = element.Label;
            label.CssClass = string.Format("{0}Label", element.CSS);

            #region Validators

            Label redStar = null;
            RequiredFieldValidatorForRating required = null;

            if (element.Required)
            {
                redStar = new Label();
                redStar.ForeColor = Color.Red;
                redStar.Text = "*";

                required = new RequiredFieldValidatorForRating();
                required.ID = string.Format("required_{0}_{1}", element.FormID, element.ID);
                required.ControlToValidate = ratingTable.ID;
                required.ValidationGroup = element.FormID.ToString();
                required.Display = ValidatorDisplay.Dynamic;
                required.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("Required"), element.Label);
            }

            PlaceHolder phValidators = new PlaceHolder();

            if (element.Required)
            {
                phValidators.Controls.Add(redStar);
                phValidators.Controls.Add(required);
            }

            #endregion

            GenerateRow(element, label, ratingTable, phValidators);
        }
    }

    internal class RequiredFieldValidatorForRating : System.Web.UI.WebControls.BaseValidator
    {
        private Table _listctrl;

        public RequiredFieldValidatorForRating()
        {
            base.EnableClientScript = false;
        }

        protected override bool ControlPropertiesValid()
        {
            Table ctrl = FindControl(ControlToValidate) as Table;

            if (ctrl != null)
            {
                _listctrl = ctrl;
                return (_listctrl != null);
            }
            
            return false;  // raise exception
        }

        protected override bool EvaluateIsValid()
        {
            foreach (TableRow row in _listctrl.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    foreach (Control control in cell.Controls)
                    {
                        if (control.GetType() == typeof(RadioButton))
                        {
                            RadioButton radio = control as RadioButton;
                            if (radio.Checked)
                            {
                                return true;
                            }
                        }
                    }
                }
            }

            return false;
        }
    }
}
